<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="门店名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入门店名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="是否含有客流设备0未包含1包含" prop="HavingFlowDevice">
        <el-select v-model="queryParams.HavingFlowDevice" placeholder="请选择是否含有客流设备0未包含1包含" clearable>
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>-->
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="daterangeCreateTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

<!--    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['energize:departments:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['energize:departments:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['energize:departments:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['energize:departments:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>-->

    <el-table v-loading="loading" :data="departmentsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="自动递增id" align="center" prop="autoId" />
      <el-table-column label="门店名称" align="center" prop="name" />
      <el-table-column label="万店掌门店ID" align="center" prop="id" />
<!--      <el-table-column label="门店编号" align="center" prop="shopId" />-->
<!--      <el-table-column label="门店状态0准备中 1正常开业中 2闭店" align="center" prop="openStatus" />-->
      <el-table-column label="设备数" align="center" prop="devCount" />
      <el-table-column label="门店地址" align="center" prop="address" />
<!--      <el-table-column label="门店区域" align="center" prop="location" />-->
      <el-table-column label="组织架构id" align="center" prop="organizeId" />
      <el-table-column label="组织架构名称" align="center" prop="organizeName" />
      <el-table-column label="拼音" align="center" prop="pinyin" />
      <el-table-column label="到期时间" align="center" prop="validateDate" />
<!--      <el-table-column label="是否含有客流设备0未包含1包含" align="center" prop="HavingFlowDevice">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.HavingFlowDevice"/>
        </template>
      </el-table-column>-->
<!--      <el-table-column label="店长id" align="center" prop="shopManagerId" />-->
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="烟草设备门店ID" align="center" prop="storeId" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleStoreUpdate(scope.row)"
            v-hasPermi="['energize:departments:edit']"
          >绑定烟草门店</el-button>
<!--          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['energize:departments:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['energize:departments:remove']"
          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="updateStoreOpen" width="400px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="请选择门店">
          <el-select v-model="form.storeId" placeholder="请选择门店" prop="storeId">
            <el-option
              v-for="item in device_infoList"
              :key="item.id"
              :label="item.company"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </el-form-item>

      </el-form>
    </el-dialog>
    <!-- 添加或修改万店掌门店列表对话框 -->
<!--    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="万店掌门店id" prop="id">
          <el-input v-model="form.id" placeholder="请输入万店掌门店id" />
        </el-form-item>
        <el-form-item label="门店名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入门店名称" />
        </el-form-item>
        <el-form-item label="门店编号" prop="shopId">
          <el-input v-model="form.shopId" placeholder="请输入门店编号" />
        </el-form-item>
        <el-form-item label="烟草设备id(门店id)" prop="storeId">
          <el-input v-model="form.storeId" placeholder="请输入烟草设备id(门店id)" />
        </el-form-item>
        <el-form-item label="设备数" prop="devCount">
          <el-input v-model="form.devCount" placeholder="请输入设备数" />
        </el-form-item>
        <el-form-item label="门店地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入门店地址" />
        </el-form-item>
        <el-form-item label="门店区域" prop="location">
          <el-input v-model="form.location" placeholder="请输入门店区域" />
        </el-form-item>
        <el-form-item label="组织架构id" prop="organizeId">
          <el-input v-model="form.organizeId" placeholder="请输入组织架构id" />
        </el-form-item>
        <el-form-item label="组织架构名称" prop="organizeName">
          <el-input v-model="form.organizeName" placeholder="请输入组织架构名称" />
        </el-form-item>
        <el-form-item label="拼音" prop="pinyin">
          <el-input v-model="form.pinyin" placeholder="请输入拼音" />
        </el-form-item>
        <el-form-item label="到期时间" prop="validateDate">
          <el-input v-model="form.validateDate" placeholder="请输入到期时间" />
        </el-form-item>
        <el-form-item label="是否含有客流设备0未包含1包含" prop="HavingFlowDevice">
          <el-radio-group v-model="form.HavingFlowDevice">
            <el-radio
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="店长id" prop="shopManagerId">
          <el-input v-model="form.shopManagerId" placeholder="请输入店长id" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>-->
  </div>
</template>

<script>
import { listDepartments, getDepartments, delDepartments, addDepartments, updateDepartments } from "@/api/energize/departments";
import { listDevice_info } from "@/api/energize/device_info";
export default {
  name: "Departments",
  dicts: ['sys_yes_no'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 万店掌门店列表表格数据
      departmentsList: [],
      // 设备信息表格数据
      device_infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      updateStoreOpen:false,
      open: false,
      // 店长id时间范围
      daterangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        HavingFlowDevice: null,
        createTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getStoreList();
    this.getList();
  },
  methods: {
    /**
     * 查询设备列表
     * */
    getStoreList(){
      listDevice_info().then(response => {
        this.device_infoList = response.rows;
      });
    }
    ,
    /** 查询万店掌门店列表列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      listDepartments(this.queryParams).then(response => {
        this.departmentsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.updateStoreOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        autoId: null,
        id: null,
        name: null,
        shopId: null,
        storeId: null,
        openStatus: null,
        devCount: null,
        address: null,
        location: null,
        organizeId: null,
        organizeName: null,
        pinyin: null,
        validateDate: null,
        HavingFlowDevice: null,
        shopManagerId: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.autoId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加万店掌门店列表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const autoId = row.autoId || this.ids
      getDepartments(autoId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改万店掌门店列表";
      });
    },
    handleStoreUpdate(row) {
      this.reset();
      const autoId = row.autoId || this.ids
      getDepartments(autoId).then(response => {
        this.form = response.data;
        this.updateStoreOpen = true;
        this.title = "绑定烟草对应的门店";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.autoId != null) {
            updateDepartments(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.updateStoreOpen=false;
              this.getList();
            });
          } else {
            addDepartments(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const autoIds = row.autoId || this.ids;
      this.$modal.confirm('是否确认删除万店掌门店列表编号为"' + autoIds + '"的数据项？').then(function() {
        return delDepartments(autoIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('energize/departments/export', {
        ...this.queryParams
      }, `departments_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
